package com.liuh.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liuh.pojo.AccInfo;
import com.liuh.service.AccService;
import com.liuh.service.impl.AccServiceImpl;
import com.liuh.util.OutMsg;

import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONUtil;

/**
 * @note 登陆处理
 * @author liuh
 * @date 2021年4月13日 上午9:47:10
 */
@SuppressWarnings("serial")
public class LoginServlet extends BaseServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String opr = req.getParameter("opr");
		if ("doLogin".equals(opr)) {
			resp.setContentType("application/json;charset=utf-8");
			AccService accService = new AccServiceImpl();
			AccInfo acc = accService.findAcc(req.getParameter("username"), req.getParameter("password"));
			Dict result = null;
			if (acc == null) {
				result = OutMsg.fail("登陆失败");
			} else {
				result = OutMsg.success("登陆成功");
				req.getSession().setAttribute("loginName", acc.getAccName());
			}
			outReps(JSONUtil.toJsonStr(result), resp);
		} else {
			req.getSession().removeAttribute("loginName");
			resp.sendRedirect("login.jsp");
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}

}
